<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `7
        1 1
        1 2
        2 4
        2 5
        1 3
        3 7
        4 6        `

        let spl = input.split('\n')
        let n = parseInt(spl.shift().trim())
        let task = spl.map(v => v.trim().split(' ').map(j => parseInt(j)))

        let t = []
        let res = 0
        let obj = {}
        for (let i = 0; i <= n; i++) {
            obj[i] = []
        }

        task.forEach((v) => {
            obj[v[0]].push(v[1])
        })
        

        for (let i = 1; i <= n; i++) {
            obj[i]=[...obj[i],...obj[i-1].filter(v=>v>=i) ].sort((a,b)=>a-b)
            if(obj[i].length){
                obj[i].shift()
                res++
            }
        }
        console.log(obj);
        console.log(res);
        
        





    </script>
</body>

</html>